package deprecated.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import util.DomainUtils;
import util.FormArgumentsException;
import domain.dao.interfaces.IDAOContactGroup;
import domain.entities.impl.Contact;
import domain.entities.impl.ContactGroup;
import domain.entities.interfaces.IContactGroup;

/**
 * Servlet implementation class SearchContactGroupServlet
 */
public class SearchContactGroupServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SearchContactGroupServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String partOfGroupName = DomainUtils.trim(request.getParameter("part_of_groupname"));

		try {
			checks(request, partOfGroupName);
			ApplicationContext context = util.SpringUtils.getApplicationContext();
			
			IDAOContactGroup daoContactGroup = (IDAOContactGroup) context.getBean("beanDAOContactGroup");
			List<IContactGroup> contactGroups = daoContactGroup.searchContactGroup(partOfGroupName);
			request.setAttribute(DomainUtils.SEARCH_CONTACT_GROUP_RESULT, contactGroups);
					
//			List<ContactGroup> contactGroups = 
//					DAOFactory.getDaoContactGroup().searchContactGroup(partOfGroupName);
//			request.setAttribute(DomainUtils.SEARCH_CONTACT_GROUP_RESULT, contactGroups);

			DomainUtils.displayInfoMessage(
					request, 
					DomainUtils.SHOW_CONTACT_GROUP_ALERT_CURRENT_COLOR, 
					"The research has finished successfully.");
		}
		catch(Exception e) {

			if((e instanceof FormArgumentsException) == false) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "An error occur on the server.");
			}
			
			
		}


		DomainUtils.keepParametersContent(request);
		request.getRequestDispatcher("pages/showAllContactGroups.jsp").forward(request, response);
	}

	private void checks(HttpServletRequest request, String partOfGroupName) throws Exception {

		HttpSession session = request.getSession(false);


		//Checking session
		if(session == null || session.getAttribute(DomainUtils.CURRENT_USER_STRING) == null) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
					"You are not currently connected. May be the session expired.");
			throw new FormArgumentsException();
		}
		
		if (partOfGroupName == null || partOfGroupName.isEmpty()) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
					"The field \"Search\" should not be empty");
			throw new FormArgumentsException();
		}
	}
}
